<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/include.jsp" %>
<%@ include file="/WEB-INF/jsp/header.jsp" %>

<script>
	function format() {
		list = $("input");
		for (var j = 0; j < list.length; j++) {
		  	if (list[j].type == "datetime-local" && list[j].value.indexOf("-") > 4)
		  			list[j].value = list[j].value.slice(0, 4) + list[j].value.slice(list[j].value.indexOf("-"));
		}
	}
	function check() {
		 var b = true;
		 var list = $("select");
		 for (var j = 0; j < list.length; j++) {
			 if (list[j].value == -1) {
			 	list[j].style.color="red";
				for(var i = 0; i < list[j].options.length; i++)
					list[j].options[i].style.color="black";
				b = false;
			 }
		 }
		 var min = document.getElementById("dep");
		 var max = document.getElementById("arr");
		 if (min.value != "" && max.value != "" && min.value > max.value) {
		 	b = false;
		 	min.style.borderColor="red";
		 	max.style.borderColor="red";
		 }
		 else {
		 	min.style.borderColor="green";
		 	max.style.borderColor="green";
		 }
		 min = document.getElementById("minp");
		 max = document.getElementById("maxp");
		 if (min.value != "" && max.value != "" && parseInt(min.value) > parseInt(max.value)) {
		 	b = false;
		 	min.style.borderColor="red";
		 	max.style.borderColor="red";
		 }
		 else {
		 	min.style.borderColor="green";
		 	max.style.borderColor="green";
		 }
		 min = document.getElementById("mins");
		 max = document.getElementById("maxs");
		 if (min.value != "" && max.value != "" && parseInt(min.value) > parseInt(max.value)) {
		 	b = false;
		 	min.style.borderColor="red";
		 	max.style.borderColor="red";
		 }
		 else {
		 	min.style.borderColor="green";
		 	max.style.borderColor="green";
		 }
		 return b;
	}
</script>
<title>Поиск рейсов</title>
<a href="main">
	<div class="mybutton">
		На главную
	</div>
</a>
<br><br><br>
<form id="searchForm" method="POST" action="trip_search" onsubmit="return check()">
	<input name="list[0]" type="datetime-local" id="dep" onchange="format()"/>
	<input name="list[1]" type="datetime-local" id="arr" onchange="format()"/>
	<select name="list[2]">
			<option value="-1" selected>Отбытие</option>
			<c:forEach items="${stops}" var="stop" varStatus="i">
				<option value="${stop.id}">${stop.name}</option>
			</c:forEach>
	</select>
	<select name="list[3]">
			<option value="-1" selected>Прибытие</option>
			<c:forEach items="${stops}" var="stop" varStatus="i">
				<option value="${stop.id}">${stop.name}</option>
			</c:forEach>
	</select>
	<br>
	<input name="list[4]" type="number" placeholder="Цена от" id="minp" data-validation-optional="true" 
		data-validation="number" data-validation-allowing="range[0;10000000]"/>
	<input name="list[5]" type="number" placeholder="Цена до" id="maxp" data-validation-optional="true" 
		data-validation="number" data-validation-allowing="range[0;10000000]"/>
	<input name="list[6]" type="number" placeholder="Количество остановок от" id="mins" data-validation-optional="true" 
		data-validation="number" data-validation-allowing="range[0;127]"/>
	<input name="list[7]" type="number" placeholder="Количество остановок до" id="maxs" data-validation-optional="true" 
		data-validation="number" data-validation-allowing="range[0;127]"/>
	<input name="list[8]" type="number" placeholder="Номер маршрута" data-validation-optional="true" 
		data-validation="number" data-validation-allowing="range[1;1024]"/>
	<input name="list[9]" type="number" placeholder="Количество мест" id="seatnum" data-validation-optional="true" 
		data-validation="number" data-validation-allowing="range[0;127]"/>
	<br>
	<c:forEach items="${comps}" var="comp" varStatus="i">
			<input type="checkbox" name="sec[${i.index}]" value="${comp.id}">${comp.name}<br>
	</c:forEach>
	<br>
	<input id="search" class="button" type="submit" value="Поиск"/>
</form>
<br>
<c:if test="${trips.size() != 0}">
<table>
<tr>
	<th>Номер рейса</th>
	<th>Номер маршрута</th>
	<th>Отправка</th>
	<th>Прибытие</th>
	<th>Количество свободных мест<p>(на весь маршрут)</p></th>
	<th>Компания</th>
	<th>Заказ?</th>
</tr>

<c:forEach items="${trips}" varStatus="i" var="trip">
	<tr>
		<td><a href="trip?id=${trip.id}"> №${trip.number}</a></td>
		<td><a href="route?id=${trip.route.id}"> №${trip.route.number}</a></td>
		<td>${strings.get(i.index * 5 + 0)} ${strings.get(i.index * 5 + 2)}</td>
		<td>${strings.get(i.index * 5 + 1)} ${strings.get(i.index * 5 + 3)}</td>
		<td>${seats.get(i.index)}</td>
		<td>${trip.company.name}</td>
		<td>${strings.get(i.index * 5 + 4)}</td>
	</tr>
</c:forEach>
</table>
</c:if>
<%@ include file="/WEB-INF/jsp/footer.jsp" %>